package com.rtx.edi.platform.field.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.api.enums.FieldValidateType;
import com.rtx.api.utils.FieldValidateUtils;
import com.rtx.edi.model.application.dto.PlApplicationDto;
import com.rtx.edi.model.application.po.PlApplication;
import com.rtx.edi.model.application.po.PlApplicationField;
import com.rtx.edi.model.field.po.PlFieldMapping;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.field.service.PlFieldMappingService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 映射规则配置表  前端控制器
 * </p>
 *
 * @author abc
 * @since 2023-03-06
 */
@Api(tags = {"字段映射规则"})
@RestController
@RequestMapping("/v1/plFieldMapping")
@CrossOrigin
public class PlFieldMappingController {

	@Autowired
	PlFieldMappingService plFieldMappingService;

    /**
     * 获取映射规则列表
     * @return
     */
    @ApiOperation(value = "映射规则列表", notes = "分页获取映射规则列表")
    @PostMapping("selectFieldMappingList")
    public EdiResponse selectPlApplicationList(@RequestBody PlFieldMapping dto){
    	PlFieldMapping po = new PlFieldMapping();
        BeanUtils.copyProperties(dto,po);
        Page<PlFieldMapping> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
        plFieldMappingService.selectPageList(pageParam,po);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * 根据ID获取映射规则
     * @param plApplicationDto
     * @return
     */
    @ApiOperation(value = "查询映射规则", notes = "根据ID查询映射规则")
    @PostMapping("selectFieldMappingById")
    public EdiResponse selectFieldMappingById(@RequestBody PlFieldMapping fieldMapping){
        return EdiResponse.success().data("item",plFieldMappingService.getById(fieldMapping.getId()));
    }
    /**
     * 根据id删除
     * @param plApplicationDto
     * @return
     */
    @ApiOperation(value = "删除映射规则", notes = "根据id删除映射规则", httpMethod = "POST")
    @PostMapping("deletePlFieldMappingById")
    public EdiResponse deletePlApplicationById(@RequestBody PlFieldMapping dto) {
        return plFieldMappingService.removeById(dto.getId()) ? EdiResponse.success() : EdiResponse.failed();
    }
    
    /**
     * 根据id列表删除
     * @param list
     * @return
     */
    @ApiOperation(value = "批量删除映射规则", notes = "根据id列表删除映射规则", httpMethod = "POST")
    @PostMapping("deletePlFieldMappingByIds")
    public EdiResponse deletePlApplicationByIds(@RequestBody List<PlFieldMapping> list) {
    	try {
    	    List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
    		return  plFieldMappingService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
    	} catch (Exception e) {
    		return EdiResponse.failed().returnMgr(e.getMessage());
    	}

    }

    /**
     * 新增映射规则
     * @param plApplicationDto
     * @return
     */
    @ApiOperation(value = "新增映射规则")
    @PostMapping("addFieldMapping")
    public EdiResponse addPlApplication(@RequestBody PlFieldMapping po){
        //验证非空字段
        FieldValidateUtils.fieldNotNull(po, FieldValidateType.INSERT);
        plFieldMappingService.insertFieldMapping("ris_edi", po);
        return  EdiResponse.success();
    }

    /**
     * 根据ID修改映射规则
     * @return
     */
    @ApiOperation(value = "修改映射规则", notes = "根据ID修改映射规则")
    @PostMapping("editFieldMappinById")
    public EdiResponse editPlApplicationById(@RequestBody PlFieldMapping po){
        return plFieldMappingService.updateById(po) ? EdiResponse.success() : EdiResponse.failed();
    }
}
